package com.china08.yxyapi.api.manage.tec.teaching.lecture.teachreport;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.esbag.YxyEsbagTeachingPerformance;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingPerformanceRepository;

/**
 * 老师 ---备授课--授课报告--分析 --排名
 * 
 */
@CrossOrigin
@RestController
public class LectureReportClassAnalyzeRankController extends YxyApiBaseController {

    @Autowired
    private YxyEsbagTeachingPerformanceRepository yxyEsbagTeachingPerformanceRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/analyze/rank/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String id = reqModel.id;
        Sort sort = new Sort(Direction.DESC, "rate").and(new Sort(Direction.ASC, "duration"));
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size, sort);

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        String classId = reqModel.classId;
        Page<YxyEsbagTeachingPerformance> page = yxyEsbagTeachingPerformanceRepository
                .findByTeacherIdAndSchoolIdAndPackIdAndClassId(userId, schoolId, id, classId, pageable);

        List<RespModel> list = new ArrayList<RespModel>();
        for (YxyEsbagTeachingPerformance yxyEsbagTeachingPerformance : page) {
            RespModel respModel = new RespModel();
            respModel.duration = yxyEsbagTeachingPerformance.getDuration();
            respModel.rate = yxyEsbagTeachingPerformance.getRate() * 100;
            respModel.studentId = yxyEsbagTeachingPerformance.getSchoolId();
            respModel.studentNick = yxyEsbagTeachingPerformance.getStudentNick();
            respModel.status = yxyEsbagTeachingPerformance.getStatus();
            list.add(respModel);
        }
        Page<RespModel> pageRes = new PageImpl<RespModel>(list, pageable, page.getTotalElements());

        return pageRes;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 授课包id
        public String id;
        // 班级ID
        public String classId;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 学生ID
        public String studentId;
        // 学生姓名
        public String studentNick;
        // 正确率
        public Double rate;
        // 用时 单位秒
        public long duration;
        // 是否提交
        public Integer status;
    }
}
